package org.study.async;

import java.util.concurrent.*;

/**
 * @author Administrator
 */
public class FutureTests {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        futureCallableTest01();
        futureCallableTest02();
        futureCallableTest03();

    }

    public static void futureCallableTest01() throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<String> result = executor.submit(() -> "Future 和 Callable 用法示例1");
        executor.shutdown();

        System.out.println("执行结果：" + result.get());
    }

    public static void futureCallableTest02() throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(() -> "Future 和 Callable 用法示例2");
        new Thread(task).start();
        System.out.println("执行结果：" + task.get());
    }

    public static void futureCallableTest03() throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        FutureTask<String> task = new FutureTask<>(() -> "Future 和 Callable 用法示例3");
        executor.submit(task);
        System.out.println("执行结果：" + task.get());
    }
}
